import java.util.concurrent.Semaphore;

public class Test24 {
    /**
     * 目的:控制信号量
     * 方法:使用Semaphore类
     * @param args
     */
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(1);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("准备申请资源");
                    semaphore.acquire();
                    System.out.println("资源申请成功");
                    Thread.sleep(1000);
                    semaphore.release();
                    System.out.println("释放资源");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        for (int i = 0; i < 6; i++) {
            Thread thread = new Thread(runnable);
            thread.start();
        }
    }
}
